/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.view.image;

import offset.nodes.client.chooser.view.NodeChooserDialog;
import offset.nodes.client.model.PanelContainer;
import offset.nodes.client.model.Server;

/**
 *
 * @author Walter Lütgenau
 */
public class RepositoryImagePanel extends javax.swing.JPanel {
    Server server;
    String imageUuid;
    PanelContainer container;

    /** Creates new form RepositoryImagePanel */
    public RepositoryImagePanel() {
        initComponents();
    }

    public void init(PanelContainer container, Server server ) {
        this.container = container;
        this.server = server;
        validateOkButton();
    }

    public String getPath() {
        return locationTextField.getText();
    }

    public String getUuid() {
        return imageUuid;
    }

    protected void validateOkButton() {
        if ( locationTextField.getText() != null && locationTextField.getText().length() > 0)
            container.enableOkButton(true);
        else
            container.enableOkButton(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        locationLabel = new javax.swing.JLabel();
        locationTextField = new javax.swing.JTextField();
        locationButton = new javax.swing.JButton();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle"); // NOI18N
        locationLabel.setText(bundle.getString("Image.Repository.location")); // NOI18N

        locationButton.setText("...");
        locationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectRepositoryLocation(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(locationLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(locationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(locationButton)
                .addContainerGap(140, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(locationLabel)
                    .addComponent(locationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(locationButton))
                .addContainerGap(211, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectRepositoryLocation(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectRepositoryLocation
        try {
            NodeChooserDialog dialog = new NodeChooserDialog(null, true, server, "//", "element(*,nodes:file)");
            dialog.setVisible(true);
  
            if (dialog.getReturnStatus() == NodeChooserDialog.RET_OK) {
                locationTextField.setText(dialog.getPath());
                imageUuid = dialog.getUuid();
                validateOkButton();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }//GEN-LAST:event_selectRepositoryLocation


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton locationButton;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JTextField locationTextField;
    // End of variables declaration//GEN-END:variables

}
